﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace QWManage.Utils
{
    public class HttpHelper
    {
        //获取页面页面内容
        public static string GetHtmlSource(string url)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            string html = php.ReadToEnd();
            return html;

        }

        public static string Submit(string url, string parameters)
        {
            string data = parameters.Replace("\\", "");
            byte[] byteArray = Encoding.UTF8.GetBytes(data);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;

            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();

            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string Message = php.ReadToEnd();

            return Message;

        }
    }
}
